package com.neuedu.weishi.controller;

import cn.hutool.core.util.StrUtil;
import com.neuedu.weishi.util.RedisUtils;
import com.neuedu.weishi.vo.ResponseData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import com.neuedu.weishi.entity.Member;
import com.neuedu.weishi.service.IMemberService;

/**
* Author lu
* Date   2025-08-26 09:11:00
*/
@Log4j2
@RequestMapping("/weishi/member/api")
@RestController
@Tag(name = "会员控制器")
public class MemberController  {

    @Autowired
    @Qualifier("memberService")
    private IMemberService memberService;

    @Autowired
    private RedisUtils redisUtils;

    /**
     *  会员注册
     * @param email 邮箱
     * @param password 密码
     * @param code 验证码
     * @return
     */
    @PostMapping("/register")
    @Operation(summary = "会员注册")
    public ResponseData register(@RequestParam(required = true) @Parameter(description = "邮箱",required = true) String email, @RequestParam(required = true) @Parameter(description = "密码",required = true) String password, @RequestParam(required = true) @Parameter(description = "验证码",required = true)String code) {
        return memberService.register(email,password,code);
    }

    /**
     * 发送注册验证码
     * @param email
     * @return
     */
    @GetMapping("/send_register_code")
    @Operation(summary = "发送注册验证码")
    public ResponseData sendRegisterCode(@RequestParam(required = true) @Parameter(description = "邮箱",required = true) String email) {
        return memberService.sendRegisterCode(email);
    }

    /**
     * 会员登录
     * @param email 邮箱
     * @param password 密码
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "会员登录")
    public ResponseData login(@RequestParam(required = true) @Parameter(description = "邮箱",required = true) String email, @RequestParam(required = true) @Parameter(description = "密码",required = true) String password) {
        return memberService.login(email,password);
    }
    /**
     * 忘记密码后重置密码
     * @param email 邮箱
     * @param newPwd 新密码
     * @param code 验证码
     * @return
     */
    @Operation(summary = "重置密码")
    @PostMapping("/reset_password")
    public ResponseData resetPassword(@RequestParam(required = true) @Parameter(description = "邮箱",required = true) String email, @RequestParam(required = true) @Parameter(description = "新密码",required = true) String newPwd, @RequestParam(required = true) @Parameter(description = "验证码",required = true) String code) {
        return memberService.resetPassword(email,code,newPwd);
    }
    /**
     * 发送忘记密码验证码
     * @param email 邮箱
     * @return
     */
    @Operation(summary = "发送忘记密码验证码")
    @PostMapping("/send_find_pwd_code")
    public  ResponseData sendFindPwdCode(@RequestParam(required = true) @Parameter(description = "邮箱",required = true) String email){
        return memberService.sendFindPwdCode(email);
    }

    /**
     * 获取会员基本信息
     * @param token
     * @return
     */
    @GetMapping("/info")
    @Operation(summary = "获取会员信息")
    public  ResponseData  getMemberInfo(String token, HttpServletRequest request){
        if (StrUtil.isEmpty(token)){
            return ResponseData.missParam("令牌token");
        }
        String tokenKey="login:token:"+token;
        String memberId=(String)redisUtils.get(tokenKey);
        if(memberId==null){
            return ResponseData.fail(999,"无效的token,请重新登录");
        }
        if(!memberService.checkToken(token)){
            return ResponseData.fail(999,"非合法令牌,无法获取用户信息");
        }
        //根据会员ID查询会员信息
        Member member=memberService.getById(memberId);
        return ResponseData.success(member);
    }

    /**
     * 设置会员头像
     * @param token 令牌
     * @param imgUrl 用户头像
     * @return
     */
    @Operation(summary = "设置头像")
    @PostMapping("/setting_head_img")
    public  ResponseData  settingHeadImg(@RequestParam(required = true) @Parameter(description ="令牌",required = true ) String token,@RequestParam(required = true) @Parameter(description ="用户头像",required = true ) String imgUrl){
        return ResponseData.success("success");
    }
    /**
     * 修改密码
     * @param token 令牌
     * @param oldPwd 旧密码
     * @param newPwd 新密码
     * @param newConfirmPwd 新确认密码
     * @return
     */
    @Operation(summary = "修改密码")
    @PostMapping("/modify_password")
    public ResponseData modifyPassword(@RequestParam(required = true) @Parameter(description ="令牌",required = true ) String token,@RequestParam(required = true) @Parameter(description ="旧密码",required = true ) String oldPwd,@RequestParam(required = true) @Parameter(description ="新密码",required = true ) String newPwd,@RequestParam(required = true) @Parameter(description = "新确认密码",required = true) String newConfirmPwd){
        return memberService.updatePassword(token,oldPwd,newPwd,newConfirmPwd);
    }

    /**
     * 获取我的视频
     * @param token 令牌
     * @return
     */
    @Operation(summary = "获取我的视频")
    @PostMapping("/get_my_videos")
    public  ResponseData getMyVideos(@RequestParam(required = true) @Parameter(description ="令牌",required = true ) String token){
        return ResponseData.success("success");
    }


}